package com.js.controller;

import com.js.mapper.AdminMapper;
import com.js.pojo.Admin;
import org.apache.taglibs.standard.lang.jstl.NullLiteral;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminMapper adminMapper;
    //跳转登录界面
    @RequestMapping("/jumpLogin")
    public String jumpLogin(HttpSession session){
        if (session.getAttribute("adminName")!=null){
            return "admin/backstage";
        }
        return "admin/adminLogin";
    }
    @RequestMapping("/jumpFalse")
    public String jumpFalse(){
        return "admin/codeFalse";
    }
    //登录时提交form表单验证的请求
    @RequestMapping("/login")
    public String login(Admin admin,HttpSession session){
        Admin queryByAccount = adminMapper.queryByAccount(admin.getAccount());
        if (queryByAccount==null){
            return "redirect:/admin/jumpFalse";
        }
        if (queryByAccount.getPassword().equals(admin.getPassword())){
            session.setAttribute("adminName",queryByAccount.getName());
            return "admin/backstage";
        }else {
            return "redirect:/admin/jumpFalse";
        }
    }
    //退出登录
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return  "../index";
    }
}
